package com.water.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.water.note.entity.Wish;
import com.water.note.mapper.WishMapper;
import com.water.note.service.WishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 愿望清单Service实现类
 */
@Service
public class WishServiceImpl extends ServiceImpl<WishMapper, Wish> implements WishService {

    @Override
    public Page<Wish> page(Page<Wish> page, Long userId, Integer status, String keyword) {
        LambdaQueryWrapper<Wish> queryWrapper = new LambdaQueryWrapper<>();
        // 根据用户ID查询
        queryWrapper.eq(Wish::getUserId, userId);
        // 根据状态查询
        if (status != null) {
            queryWrapper.eq(Wish::getStatus, status);
        }
        // 根据关键字查询
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(Wish::getTitle, keyword)
                    .or()
                    .like(Wish::getDescription, keyword);
        }
        // 按创建时间降序排序
        queryWrapper.orderByDesc(Wish::getCreateTime);
        return page(page, queryWrapper);
    }

    @Override
    public boolean updateProgress(Long id, Integer progress) {
        // 参数校验
        if (id == null || progress == null || progress < 0 || progress > 100) {
            return false;
        }
        // 查询愿望是否存在
        Wish wish = getById(id);
        if (wish == null) {
            return false;
        }
        // 更新进度
        wish.setProgress(progress);
        // 如果进度为100，则状态更新为已实现
        if (progress == 100) {
            wish.setStatus(2); // 2-已实现
        }
        return updateById(wish);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        // 参数校验
        if (id == null || status == null || status < 0 || status > 3) {
            return false;
        }
        // 查询愿望是否存在
        Wish wish = getById(id);
        if (wish == null) {
            return false;
        }
        // 更新状态
        wish.setStatus(status);
        // 如果状态为已实现，则进度更新为100
        if (status == 2) {
            wish.setProgress(100);
        }
        return updateById(wish);
    }

    @Override
    public Map<String, Object> getOverviewData(Long userId) {
        Map<String, Object> result = new HashMap<>();
        
        // 查询愿望清单总数
        LambdaQueryWrapper<Wish> countQuery = new LambdaQueryWrapper<>();
        countQuery.eq(Wish::getUserId, userId);
        long total = count(countQuery);
        result.put("count", total);
        
        // 计算平均进度
        if (total > 0) {
            // 查询所有愿望的进度
            LambdaQueryWrapper<Wish> progressQuery = new LambdaQueryWrapper<>();
            progressQuery.eq(Wish::getUserId, userId).select(Wish::getProgress);
            List<Wish> wishes = list(progressQuery);
            
            // 计算平均进度
            int totalProgress = wishes.stream().mapToInt(Wish::getProgress).sum();
            int avgProgress = totalProgress / wishes.size();
            result.put("progress", avgProgress);
        } else {
            result.put("progress", 0);
        }
        
        return result;
    }
    
    @Override
    public List<Wish> getRecentWishes(Long userId, int limit) {
        // 查询最近的愿望清单
        LambdaQueryWrapper<Wish> query = new LambdaQueryWrapper<>();
        query.eq(Wish::getUserId, userId)
             .orderByDesc(Wish::getCreateTime)
             .last("LIMIT " + limit);
        return list(query);
    }
} 